package com.cqut.forum.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cqut.forum.entity.ArticleGroup;
import com.cqut.forum.entity.PictureGroup;
import com.cqut.forum.entity.User;
import com.cqut.forum.service.ArticleGroupService;
import com.cqut.forum.service.PictureGroupService;

@Controller
public class GroupAction {
	@Resource
	private ArticleGroupService articleGroupService;
	@Resource
	private PictureGroupService pictureGroupService;
	
	@RequestMapping("/group/get_groups")
	@ResponseBody
	public List<Map<String, Object>> getGroups(HttpServletRequest request,String moduleName){
		List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
		User user = (User)request.getSession().getAttribute("user");
		if(moduleName.equals("picture")){
			List<PictureGroup> groups = pictureGroupService.getGroups(user.getUserId());
			
			for(PictureGroup pg:groups){
				Map<String, Object> m = new HashMap<String, Object>();
				m.put("groupId", pg.getPictureGroupId());
				m.put("pictureSrc", pg.getMarkPictureUrl());
				m.put("groupName", pg.getPictureGroupName());
				m.put("number", pg.getPictureNumber());
				m.put("password", pg.getPicturePassword());
				m.put("isPublic", pg.getIsPublic()?"公开":"加密");
				result.add(m);
			}
		}
		else if(moduleName.equals("article")){
			List<ArticleGroup> groups = articleGroupService.getByOwnerId(user.getUserId(), 0, 500);
			
			for(ArticleGroup ag:groups){
				Map<String, Object> m = new HashMap<String, Object>();
				m.put("groupId", ag.getArticleGroupId());
				m.put("groupName", ag.getArticleGroupName());
				m.put("number", ag.getArticleNumber());
				m.put("password", ag.getArticlePassword());
				m.put("isPubic", ag.getIsPublic());
				result.add(m);
			}
		}
		return result;
	}
	
	@RequestMapping("/group/change_password")
	@ResponseBody
	public String changeGroupPassword(HttpServletRequest request,String password,
			int groupId,String module){
		if(module.equals("picture")){
			PictureGroup pg = pictureGroupService.getGroupById(groupId);
			pg.setIsPublic(password.equals("")?true:false);
			pg.setPicturePassword(password);
			pictureGroupService.update(pg);
		}
		else if(module.equals("article")){
			ArticleGroup ar = articleGroupService.getGroupById(groupId);
			ar.setIsPublic(password.equals("")?true:false);
			ar.setArticlePassword(password);
			articleGroupService.updateArticleGroup(ar);
		}
		return "true";
	}
	
	@RequestMapping("/group/delete_group")
	@ResponseBody
	public String deleteGroup(int groupId,String moduleName){
		if(moduleName.equals("picture")){
			pictureGroupService.delete(groupId);
		}
		else if(moduleName.equals("article")){
			articleGroupService.deleteById(groupId);
		}
		return "true";
	}
}
